/**
 * 
 */
package com.an.threadtest.procomu;

import java.util.LinkedList;

/**
 * @author Administrator
 *
 *         存放请求的容器
 */
public class RequestContainer {

	/**
	 * 存放请求的列表
	 */
	private final LinkedList<Request> buffer;
	// 容器允许的最大值
	private int MAX;

	public RequestContainer(int MAX) {
		this.buffer = new LinkedList<Request>();
		this.MAX = MAX;
	}
	
	/**
	 * @return 容器当前的大小
	 */
	public int size() {
		return buffer.size();
	}
	
	/**
	 * 将审生产的请求放入容器
	 * @param request 请求
	 * @throws InterruptedException
	 */
	public synchronized void put(Request request) throws InterruptedException {
		while(buffer.size() >= MAX) {
			wait();
		}
		System.out.println("放入请求：" + request + "-------------->>>当前线程：" + Thread.currentThread().getName());
		buffer.add(request);
		//通知消费者线程
		notifyAll();
	}
	
	/**
	 * @return 请求
	 * @throws InterruptedException 
	 */
	public synchronized Request get() throws InterruptedException {
		while(buffer.size() <= 0) {
			System.out.println("***获取请求失败，没有足够的请求，等待生产***---【" + Thread.currentThread().getName() + "】");
			wait();
		}
		Request r = buffer.removeFirst();
		notifyAll();
		return r;
	}

}
